﻿using MediatR;
using SmartApplication.Api.Applient.Command.Process;
using SmartApplication.Api.Dto;
using SmartApplication.ApiCode;
using SmartApplication.Domain.Entity;
using SmartApplication.Infrastructure.Instrument;

namespace SmartApplication.Api.Applient.CommandHandler.Process
{
    public class ListProcessHandler : IRequestHandler<ListProcessCommand, ApiResult<List<ProcessDto>>>
    {
        public ListProcessHandler(IBaseRepository<ProcessingModel> processRepository,IBaseRepository<CorporateModel> CorporRepository,IBaseRepository<FoodSafetyMode> foodRepository)
        {
            ProcessRepository = processRepository;
            this.CorporRepository = CorporRepository;
            FoodRepository = foodRepository;
        }

        public IBaseRepository<ProcessingModel> ProcessRepository { get; }
        public IBaseRepository<CorporateModel> CorporRepository { get; }
        public IBaseRepository<FoodSafetyMode> FoodRepository { get; }

        /// <summary>
        /// 显示深加工信息成功
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<List<ProcessDto>>> Handle(ListProcessCommand request, CancellationToken cancellationToken)
        {
            var res=new ApiResult<List<ProcessDto>>();

            var pro = ProcessRepository.GetAll().Where(x => x.SourceId == request.SourceId);
            var cor = CorporRepository.GetAll();
            var food = FoodRepository.GetAll();


            var list = from p in pro
                       join c in cor on p.CorporateId equals c.Id
                       join f in food on p.FoodSafetyId equals f.Id
                       select new ProcessDto()
                       {
                           CorporateId = p.CorporateId,
                           Id = p.Id,
                           SourceId = p.SourceId,
                           ChargeName = p.ChargeName,
                           Corporate = c.Corporate,
                           CorporateName = c.CorporateName,
                           CorporatePic = c.CorporatePic,
                           FoodSafetyId = p.FoodSafetyId,
                           Team = p.Team,
                           Pic = f.Pic
                       };
            res.Code = EnumCode.Success;
            res.Message = "显示深加工信息成功";
            res.Data = list.ToList();
            return res;
        }
    }
}
